import { defineStore } from 'pinia'
import { 
  getActivityList, 
  createActivity, 
  updateActivity, 
  deleteActivity, 
  getActivityDetail,
  recommendActivity,
  updateActivityStatus,
  pageActivities
} from '@/api/activity'

export const useActivityStore = defineStore('activity', {
  state: () => ({
    activityList: [],
    total: 0,
    activity: {},
    loading: false
  }),
  
  actions: {
    // 获取活动列表
    async fetchActivityList(params) {
      this.loading = true
      try {
        const response = await getActivityList(params)
        this.activityList = response.data || []
        this.loading = false
        return Promise.resolve(response.data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 分页获取活动
    async pageActivities(current, size, params) {
      this.loading = true
      try {
        const response = await pageActivities(current, size, params)
        this.activityList = response.data.records || []
        this.total = response.data.total || 0
        this.loading = false
        return Promise.resolve(response.data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 获取活动详情
    async getActivityDetail(id, userId) {
      this.loading = true
      try {
        const response = await getActivityDetail(id, userId)
        this.activity = response.data
        this.loading = false
        return Promise.resolve(response.data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 创建活动
    async createActivity(data) {
      try {
        const response = await createActivity(data)
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 更新活动
    async updateActivity(id, data) {
      try {
        const response = await updateActivity(id, data)
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 删除活动
    async deleteActivity(id) {
      try {
        const response = await deleteActivity(id)
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 推荐活动
    async recommendActivity(id, isRecommended) {
      try {
        const response = await recommendActivity(id, isRecommended)
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 更新活动状态
    async updateActivityStatus(id, status) {
      try {
        const response = await updateActivityStatus(id, status)
        return Promise.resolve(response.data)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  }
}) 